#include "BreakthroughGameState.h"


BreakthroughGameState::BreakthroughGameState()
{ 
	m_currentPlayer = 0;
	m_numMoves = 0;
} 


BreakthroughGameState::~BreakthroughGameState()
{

}

void BreakthroughGameState::startState(std::vector< std::vector<char> >& grid)
{
	GameState();
	m_rules.initPlayers(grid, m_players[0], m_players[1]);
}

std::vector<Move> BreakthroughGameState::getLegalMoves(std::vector< std::vector<char> >& grid)
{
	std::vector<Move> legalMoves;
	std::vector<Piece*> pieces = m_players[m_currentPlayer].getPieces();
	for (unsigned int i = 0; i < pieces.size(); ++i)
	{
		int fX = pieces[i]->getPosX(), fY = pieces[i]->getPosY(), tX = fX, tY = fY;
		if (m_currentPlayer == 0)
		{
			tY = fY + 1;
			if (m_rules.isLegal(m_currentPlayer, grid, fX, fY, tX, tY))
			{
				Move m;
				m.fromX = fX;
				m.fromY = fY;
				m.toX = tX;
				m.toY = tY;
				legalMoves.push_back(m);
			}
			tX = fX - 1;
			if (m_rules.isLegal(m_currentPlayer, grid, fX, fY, tX, tY))
			{
				Move m;
				m.fromX = fX;
				m.fromY = fY;
				m.toX = tX;
				m.toY = tY;
				legalMoves.push_back(m);
			}
			tX = fX + 1;
			if (m_rules.isLegal(m_currentPlayer, grid, fX, fY, tX, tY))
			{
				Move m;
				m.fromX = fX;
				m.fromY = fY;
				m.toX = tX;
				m.toY = tY;
				legalMoves.push_back(m);
			}
		}
		else if (m_currentPlayer == 1)
		{
			tY = fY - 1;
			if (m_rules.isLegal(m_currentPlayer, grid, fX, fY, tX, tY))
			{
				Move m;
				m.fromX = fX;
				m.fromY = fY;
				m.toX = tX;
				m.toY = tY;
				legalMoves.push_back(m);
			}
			tX = fX - 1;
			if (m_rules.isLegal(m_currentPlayer, grid, fX, fY, tX, tY))
			{
				Move m;
				m.fromX = fX;
				m.fromY = fY;
				m.toX = tX;
				m.toY = tY;
				legalMoves.push_back(m);
			}
			tX = fX + 1;
			if (m_rules.isLegal(m_currentPlayer, grid, fX, fY, tX, tY))
			{
				Move m;
				m.fromX = fX;
				m.fromY = fY;
				m.toX = tX;
				m.toY = tY;
				legalMoves.push_back(m);
			}
		}
	}
	return legalMoves;
}

void BreakthroughGameState::movePiece(std::vector< std::vector<char> >& grid, int fromX, int fromY, int toX, int toY)
{
	if (m_rules.isLegal(m_currentPlayer, grid, fromX, fromY, toX, toY))
	{
		GameState::movePiece(grid, fromX, fromY, toX, toY);
	}
	else 
		std::cout << "Illegal move.\n";
}